package jp.co.dailyReport.weeklyReport.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;

/**
 * 週報IDの最大値を取得するクラスです。
 */
public class WeeklyReportLoadMaxIDExecuter implements Executer {

	private static Logger log = Logger.getLogger(WeeklyReportLoadMaxIDExecuter.class);

	/**
	 * 週報IDの最大値を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 週報IDの最大値
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select max(weeklyReportId) as n from weeklyReport";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			rset = pstmt.executeQuery();
			if(rset.next())
			 answer.add(String.valueOf(rset.getInt("n")));
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (String[])answer.toArray(new String[0]);
	}

}